package listener;

import bean.RoomBean;
import com.corundumstudio.socketio.AckRequest;
import com.corundumstudio.socketio.BroadcastOperations;
import com.corundumstudio.socketio.SocketIOClient;
import com.corundumstudio.socketio.SocketIOServer;
import com.corundumstudio.socketio.listener.DataListener;
import config.EventConfig;
import test.SendPlcData;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description
 * @Author szw
 * @Data 2019-12-25 13:53
 * @Version 1.0
 */

public class RoomListener implements DataListener<RoomBean> {

    private SocketIOServer server;

    private List<SocketIOClient> clientList = new ArrayList<>();

    public RoomListener(SocketIOServer server,List<SocketIOClient> clientList){
        this.server = server;
        this.clientList = clientList;
    }

    @Override
    public void onData(SocketIOClient client, RoomBean data, AckRequest ackSender) throws Exception {

        String roomName = data.getRoomName();//获取房间的唯一标识
        BroadcastOperations bo = server.getRoomOperations(roomName);//根据房间唯一标识，获取向指定房间发送消息的bo
        String option = data.getOption();//获取前端传来的操作类型
        if("join".equals(option)){
            // 每次有人客户端进来，就存储客户端信息
            SendPlcData.clientList.add(client);
            System.out.println("{}进入房间"+client.getSessionId());
            //客户端加入房间
            client.joinRoom(data.getRoomName());
            //发送进入房间消息到所有目前在线的客户端
//            bo.sendEvent(EventConfig.TARGETEVENT,data.getUserName().concat("进入房间"));
            //向当前进入房间的客户端发送消息,一般用于获取消息历史记录
            server.getClient(client.getSessionId()).sendEvent(EventConfig.TARGETEVENT, "大美山西欢迎您");
        }else{
            System.out.println("{}走错了"+client.getSessionId());
            server.getClient(client.getSessionId()).sendEvent(EventConfig.TARGETEVENT, "走错了,小老弟~");
        }

    }
}
